#######################################################################
#Name of code file: public_opinion_analysis.R

#Data In: samoha_clean.xlsx

#Data Out: Figure A1
######################################################################

# ----------------------------------------------------------------------
# load all relevant packages
# ----------------------------------------------------------------------

## If you don't have packages installed, uncomment and install
## install.packages("tidyverse")
## install.packages("ggplot2")
## install.packages("stargazer")
## install.packages("xtable")
## install.packages("texreg")
## install.packages("readstata13")
## install.packages("estimatr")
## install.packages("naniar")
## install.packages("lfe")
## install.packages("multiwayvcov")
## install.packages("lmtest")
## install.packages("xlsx")
## install.packages("readxl")

library("tidyverse")
library("ggplot2")
library("stargazer")
library("xtable")
library("texreg")
library("readstata13")
library("estimatr")
library("naniar")
library("lfe")
library("multiwayvcov")
library("lmtest")
# library("xlsx") Doesn't work
library("readxl")

## On the computer used to generate the results of this script, below is what is
## printed to the console when sessionInfo() is run
## sessionInfo()

## > sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.1

## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base

## other attached packages:
##  [1] readxl_1.3.1       xlsx_0.6.5         lmtest_0.9-39      zoo_1.8-9
##  [5] multiwayvcov_1.2.3 lfe_2.8-7.1        Matrix_1.3-4       naniar_0.6.1
##  [9] estimatr_0.30.4    readstata13_0.10.0 texreg_1.37.5      xtable_1.8-4
## [13] stargazer_5.2.2    forcats_0.5.1      stringr_1.4.0      dplyr_1.0.7
## [17] purrr_0.3.4        readr_2.1.1        tidyr_1.1.4        tibble_3.1.6
## [21] ggplot2_3.3.5      tidyverse_1.3.1

## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.7       lubridate_1.8.0  lattice_0.20-45  xlsxjars_0.6.1
##  [5] assertthat_0.2.1 utf8_1.2.2       R6_2.5.1         cellranger_1.1.0
##  [9] backports_1.4.0  reprex_2.0.1     visdat_0.5.3     httr_1.4.2
## [13] pillar_1.6.4     rlang_0.4.12     rstudioapi_0.13  munsell_0.5.0
## [17] broom_0.7.10     compiler_4.1.2   modelr_0.1.8     pkgconfig_2.0.3
## [21] tidyselect_1.1.1 fansi_0.5.0      crayon_1.4.2     tzdb_0.2.0
## [25] dbplyr_2.1.1     withr_2.4.3      grid_4.1.2       jsonlite_1.7.2
## [29] gtable_0.3.0     lifecycle_1.0.1  DBI_1.1.1        magrittr_2.0.1
## [33] scales_1.1.1     cli_3.1.0        stringi_1.7.6    fs_1.5.2
## [37] xml2_1.3.3       ellipsis_0.3.2   generics_0.1.1   vctrs_0.3.8
## [41] boot_1.3-28      sandwich_3.0-1   Formula_1.2-4    tools_4.1.2
## [45] glue_1.5.1       hms_1.1.1        parallel_4.1.2   colorspace_2.0-2
## [49] rvest_1.0.2      rJava_1.0-6      haven_2.4.3


# Important Notice: The code below includes Hebrew text.
# If you are using a PC operating system, this text might appear as gibberish.
# To address this potential problem, please be sure to reopen the script with suitable encoding.
# To do so, select File > Reopen with Encoding... > and the select UTF-8 (System default).
# Doing so should address any problems with the usage of Hebrew text in the code below.

# ----------------------------------------------------------------------
# Read data
# ----------------------------------------------------------------------

## Uncomment and set working directory to replication folder
## setwd("folder")

samoha <- read_xlsx("data/samoha_clean.xlsx")
head(samoha)
names(samoha)

# ----------------------------------------------------------------------
# create clean data
# ----------------------------------------------------------------------

samoha_clean <- samoha %>%
 mutate(.,
        religion = case_when(
         religion == "נוצרי" ~ "Christian",
         religion == "מוסלמי" ~ "Muslim",
         religion == "דרוזי" ~ "Druze"
        ),
        religiosity = case_when(
         religiosity == "לא דתי" ~ 1,
         religiosity == "דתי במידה מסוימת" ~ 2,
         religiosity == "דתי" ~ 3,
         religiosity == "דתי מאוד" ~ 4
        ),
        bedouin = case_when(
         bedouin == "כן" ~ 1,
         bedouin == "לא" ~ 0
        ),
        hh_spend = case_when(
         hh_spend == "הרבה מתחת לממוצע" ~ 1,
         hh_spend == "קצת מתחת לממוצע"  ~ 2,
         hh_spend == "כמו הממוצע" ~ 3,
         hh_spend == "קצת מעל לממוצע" ~ 4,
         hh_spend == "הרבה מעל לממוצע" ~ 5
         ), # Note household spending is coded from low to high relative spending
        male = case_when(
         sex == "גבר"~1,
         sex == "אשה" ~ 0
        ),
        triangle_worry = case_when(
         triangle == "לא חושש" ~ 1,
         triangle == "לא כל-כך חושש" ~ 2,
         triangle == "חושש במידה מסוימת" ~ 3,
         triangle == "חושש" ~ 4
        ), # Note that this variable is coded from low to high levels of worry re:triangle land swap
        triangle_worry_fac = case_when(
         triangle_worry == 1 ~ "No",
         triangle_worry == 2 ~ "Not So Much",
         triangle_worry == 3 ~ "A Bit",
         triangle_worry == 4 ~ "Yes"
        ),
        maritial_stat = case_when(
         maritial_stat == "נשוי" ~ "Married",
         maritial_stat == "רווק" ~ "Single",
         maritial_stat == "אלמן גרוש/פרוד" ~ "Divorced"
        ),
        triagnle_res = ifelse(area == "משולש", 1, 0),
        Residence = ifelse(triagnle_res == 1, "Triangle", "Non-Triangle"))

samoha_clean$age <- as.numeric(samoha_clean$age)

# ----------------------------------------------------------------------
# Plot Data
# ----------------------------------------------------------------------
samoha_clean <- samoha_clean %>%
 mutate(.,
        worry_bin = case_when(
         triangle_worry_fac == "No" ~ "No",
         triangle_worry_fac == "Not So Much" ~ "No",
         triangle_worry_fac == "A Bit" ~ "Yes",
         triangle_worry_fac == "Yes" ~ "Yes"
        ))

samoha_clean %>%
 filter(.,
        !is.na(worry_bin)) %>%
ggplot(.) +
 stat_count(aes(x=worry_bin, y=..prop.., group=Residence), fill="dodgerblue3") +
 facet_grid(~Residence)+
 xlab("Worry Triangle will be Transferred to Palestine?")+
 ylab("Percentage") +
 theme(text = element_text(size = 10, family = "Times"),
       legend.key=element_blank(),
       panel.grid.major = element_blank(),
       axis.text.x = element_text(size = 10),
       plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank(),
       axis.line = element_line(colour = "black"))
ggsave("plots/figure_A1.pdf", width = 6.5, height = 4)

